package se19team1e.dao.impl.property;

import java.util.ArrayList;
import java.util.List;

import se19team1e.dao.abstrakt.jaas.JobAdvertDAO;
import se19team1e.dao.abstrakt.property.PropertyDAO;
import se19team1e.entity.JobAdvertDTO;
import se19team1e.exception.DataAccessException;

/**
 * Concrete class implementation of the Property DAO, Property DAO is handling
 * job advert in this implementation.
 * 
 * @author arun
 * 
 */
public class JobAdvertPropertyDAO extends PropertyDAO<JobAdvertDTO> implements
		JobAdvertDAO {
	/**
	 * Set default job advert properties as null
	 */
	private String jobAdvertProperties = null;

	/**
	 * Constructor that calls the super class PropertyDAO to create Job Advert
	 * Property DAO
	 * 
	 * @param jobAdvertProperties
	 */
	public JobAdvertPropertyDAO(String jobAdvertProperties) {
		super(jobAdvertProperties);
		this.jobAdvertProperties = jobAdvertProperties;
	}

	/**
	 * Convert DTO into properties given the Job Advert DTO object
	 * 
	 * @return true if create Job Advert DTO is successful)
	 * @throws DataAccessException
	 */
	@Override
	public boolean create(JobAdvertDTO dtoType) throws DataAccessException {

		convertDTOIntoProperties(dtoType);
		persist();
		return true;
	}

	/**
	 * 
	 * Deletes an Job Advert object given its ID
	 * 
	 * @param id
	 *            of the Job Advert object to be deleted
	 * @return true if delete is successful
	 * @throws DataAccessException
	 * 
	 */
	@Override
	public boolean delete(Object id) throws DataAccessException {

		JobAdvertDTO jobAdvertDTO = retrieveById(id);
		jobAdvertDTO.setDeleted(true);
		convertDTOIntoProperties(jobAdvertDTO);
		persist();

		return true;
	}

	/**
	 * DAO will retrieve all Job Advert objects in the persistence storage.
	 * 
	 * @return list of Job Advert DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public List<JobAdvertDTO> retrieveAll() throws DataAccessException {
		super.loadProperties(jobAdvertProperties);
		List<JobAdvertDTO> jobAdvertDTOList = new ArrayList<JobAdvertDTO>();
		int lastSequence = getLastSequence();
		JobAdvertDTO jobAdvertDTO = null;
		for (int currentSequence = 1; currentSequence <= lastSequence; currentSequence++) {
			jobAdvertDTO = new JobAdvertDTO();
			populateValuesIntoDTO(String.valueOf(currentSequence), jobAdvertDTO);
			jobAdvertDTOList.add(jobAdvertDTO);

		}
		return jobAdvertDTOList;
	}

	/**
	 * DAO will retrieve Job Advert objects in the persistence storage based on
	 * the id
	 * 
	 * @param id
	 *            unique id of the object
	 * @return list of Job Advert DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public JobAdvertDTO retrieveById(Object id) throws DataAccessException {

		JobAdvertDTO jobAdvertDTO = new JobAdvertDTO();
		populateValuesIntoDTO((String) id, jobAdvertDTO);
		return jobAdvertDTO;
	}

	/**
	 * DAO will update Job Advert objects in the persistence storage based
	 * 
	 * @param dto
	 *            Job Advert DataTransfer Object
	 * @return list of Job Advert DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public JobAdvertDTO update(JobAdvertDTO dto) throws DataAccessException {

		convertDTOIntoProperties(dto);
		persist();
		return retrieveById(dto.getId());
	}

}
